package com.example.upload.tool;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据分片工具类
 */
public class PartitionUtil {

    public static <T> List<List<T>> partition(List<T> targetList, int frequencyCount) {
        List<List<T>> partitionItem = new ArrayList<>();
        if (targetList.size() > frequencyCount) {
            int partitionCount = targetList.size() / frequencyCount;
            for (int i = 0; i < partitionCount; i++) {
                partitionItem.add(targetList.subList(i * frequencyCount, (i + 1) * frequencyCount));
            }
            if (partitionCount * frequencyCount != targetList.size()) {
                partitionItem.add(targetList.subList(partitionCount * frequencyCount, targetList.size()));
            }
        } else {
            partitionItem.add(targetList);
        }
        return partitionItem;
    }

}
